<template>
    <el-dialog :title="dialogTitle" :visible="showDialog" @close="close">
        <el-form ref="dialogRef" :model="formData" label-width="120px" :rules="rules">
            <el-form-item label="部门名称" prop="name">
                <el-input v-model="formData.name" placeholder="2-10个字符" style="width: 80%" size="mini" />
            </el-form-item>
            <el-form-item label="部门编码" prop="code">
                <el-input v-model="formData.code" placeholder="2-10个字符" style="width: 80%" size="mini" />
            </el-form-item>
            <el-form-item label="部门负责人" prop="managerId">
                <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width: 80%" size="mini">
                    <!-- 下拉选项 循环 负责人数据 label表示显示的字段 value 存储字段 -->
                    <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="部门介绍" prop="introduce">
                <el-input v-model="formData.introduce" placeholder="1-100个字符" type="textarea" size="mini" :rows="4"
                    style="width: 80%" />
            </el-form-item>
            <el-form-item>
                <!-- 按钮 -->
                <el-row type="flex" justify="center">
                    <el-col :span="12">
                        <el-button size="mini" type="primary" @click="submit">确定</el-button>
                        <el-button size="mini" @click="close">取消</el-button>
                    </el-col>
                </el-row>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {
    getDepartmentAPI, getManagerListAPI, addDepartmentAPI,
    getDepartmentDetailAPI, editDepartmentAPI
} from "@/api/department.js"
import { Loading } from 'element-ui';
export default {
    props: {
        showDialog: {   // 控制弹窗显示
            type: Boolean,
            require: true,
            default: false
        },
        parentId: { // 父级部门id
            type: Number,
            default: null
        },
        isEdit: {   // 是否编辑状态
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            // 表单数据
            formData: {
                name: '',   // 部门名称
                code: '',   // 部门编码
                managerId: '',  // 部门负责人
                introduce: '',  // 部门介绍
                pid: '' // 部门父级部门id
            },
            managerList: [], // 负责人列表
            // 表单校验规则
            rules: {
                name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
                {
                    min: 2, max: 10, message: '部门名称的长度为2-10个字符', trigger: 'blur'
                }, {
                    trigger: 'blur',
                    // 自定义校验模式
                    validator: async (rule, value, callback) => {
                        // value就是输入的部门名称
                        let result = await getDepartmentAPI()
                        if (this.isEdit && this.formData.id) {    // 编辑状态排除
                            result = result.filter(item => item.id !== this.formData.id)
                        }
                        // result数组中是否存在 value值
                        if (result.some(item => item.name === value)) {
                            callback(new Error('部门中已经有该名称了'))
                        } else {
                            callback()
                        }
                    }
                }],
                code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
                {
                    min: 2, max: 10, message: '部门编码的长度为2-10个字符', trigger: 'blur'
                }, {
                    trigger: 'blur',
                    // 自定义校验模式
                    validator: async (rule, value, callback) => {
                        // value就是输入的编码
                        let result = await getDepartmentAPI()
                        if (this.isEdit && this.formData.id) {    // 编辑状态排除
                            result = result.filter(item => item.id !== this.formData.id)
                        }
                        // result数组中是否存在 value值
                        if (result.some(item => item.code === value)) {
                            callback(new Error('部门中已经有该编码了'))
                        } else {
                            callback()
                        }
                    }
                }],
                managerId: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' },],
                introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
                    min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
                }]
            }
        }
    },
    computed: {
        dialogTitle() { // 弹窗标题
            return this.isEdit ? '编辑部门' : '新增部门'
        }
    },
    created() {
        this.getManagerList()
    },
    methods: {
        // 弹窗关闭
        close() {
            this.formData = {
                name: '',   // 部门名称
                code: '',   // 部门编码
                managerId: '',  // 部门负责人
                introduce: '',  // 部门介绍
                pid: '' // 部门父级部门id
            }
            this.$emit('update:showDialog', false)
        },
        // 获取负责人
        async getManagerList() {
            this.managerList = await getManagerListAPI()
        },
        // 确认
        submit() {
            this.$refs.dialogRef.validate(async (isOK) => {
                if (isOK) {
                    if (this.isEdit) {    // 编辑
                        await editDepartmentAPI(this.formData)
                    } else {    // 新增
                        await addDepartmentAPI({ ...this.formData, pid: this.parentId })
                    }
                    // 通知父组件更新
                    this.$emit('updateDepartment')
                    // 提示消息
                    this.$message.success(`${this.dialogTitle}成功`)
                    this.close()
                }
            });
        },
        // 获取部门详情
        async getDepartmentDetail() {
            this.formData = await getDepartmentDetailAPI(this.parentId)
        }
    }
}
</script>